package com.wonderful.mall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wonderful.common.constant.AuthServerConstant;
import com.wonderful.common.utils.R;
import com.wonderful.mall.auth.feign.MemberFeignService;
import com.wonderful.mall.auth.util.HttpUtils;
import com.wonderful.common.vo.MemberRespVo;
import com.wonderful.mall.auth.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

@Controller
public class OAuth2Controller {
    @Resource
    MemberFeignService memberFeignService;
    @GetMapping("/oauth2/success")
    public String Gitee(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换取token
        HashMap<String, String> body = new HashMap<>();
        body.put("client_id","89a7122bc1092c5b342e99c49b69dc2fc745f23edb60f25fc0edd961165fea14");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://auth.wonderfulmall.com/oauth2/success");
        body.put("client_secret","f8053dfa106edd95c2138110c9ec97976df880c9fb9cb986c80ec93f4891e3e1");
        body.put("code",code);
        HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), body, new HashMap<>());
        if (response.getStatusLine().getStatusCode()==200){
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            //如果当前用户第一次登录，自动注册(为当前社交用户生成一个会员信息账号)
            R oauthLogin = memberFeignService.oauthLogin(socialUser);
            if (oauthLogin.getCode() == 0) {
                MemberRespVo data = oauthLogin.getData("data", new TypeReference<MemberRespVo>() {});
                //1、第一次使用session，命令浏览器保存卡号，JSESSIONID这个cookie
                //以后浏览器访问哪个网站就会带上这个网站的cookie
                //默认发的令牌。当前域（解决子域session共享问题）
                //使用JSON的序列化方式来序列化对象到Redis中
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                //2、登录成功跳回首页
                return "redirect:http://wonderfulmall.com";
            } else {

                return "redirect:http://auth.wonderfulmall.com/login.html";
            }
        }else {
            return "redirect:http://auth.wonderfulmall.com/login.html";
        }
    }

    @GetMapping("/oauth2/weibo/success")
    public String Weibo(@RequestParam("code") String code, HttpSession session) throws Exception {
        //根据code换取token
        HashMap<String, String> body = new HashMap<>();
        body.put("client_id","814422731");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://auth.wonderfulmall.com/oauth2/weibo/success");
        body.put("client_secret","8c9708a1d56c98f4bfd37a54813cb873");
        body.put("code",code);
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com", "/oauth2/access_token", "post", new HashMap<>(), body, new HashMap<>());
        if (response.getStatusLine().getStatusCode()==200){
            String json = EntityUtils.toString(response.getEntity());
            SocialUser socialUser = JSON.parseObject(json, SocialUser.class);
            //如果当前用户第一次登录，自动注册(为当前社交用户生成一个会员信息账号)
            R oauthLogin = memberFeignService.oauthLogin(socialUser);
            if (oauthLogin.getCode() == 0) {
                MemberRespVo data = oauthLogin.getData("data", new TypeReference<MemberRespVo>() {});
                System.out.println("登录成功："+data);
                //1、第一次使用session，命令浏览器保存卡号，JSESSIONID这个cookie
                //以后浏览器访问哪个网站就会带上这个网站的cookie
                //TODO 1、默认发的令牌。当前域（解决子域session共享问题）
                //TODO 2、使用JSON的序列化方式来序列化对象到Redis中
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                //2、登录成功跳回首页
                return "redirect:http://wonderfulmall.com";
            } else {

                return "redirect:http://auth.wonderfulmall.com/login.html";
            }
        }else {
            return "redirect:http://auth.wonderfulmall.com/login.html";
        }
    }
}
